﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class ProductTypeController : Controller
    {
        private IProductTypeBLL _productTypeBLL;
        private UnmanContexts _unamnContexts;

        public ProductTypeController(
            IProductTypeBLL productTypeBLL
            , UnmanContexts unmanContexts
            )
        {
            _productTypeBLL = productTypeBLL;
            _unamnContexts = unmanContexts;
        }


        public IActionResult ListView()
        {
            return View();
        }
        public IActionResult CreateProductTypeView()
        {
            return View();
        }
        public IActionResult UpdateProductTypeView()
        {
            return View();
        }


        #region 添加(CreateProductType)
        [HttpPost]
        public IActionResult CreateProductType([FromForm] ProductType productType)
        {
            //返回结果封装
            ReturnResult result = new ReturnResult();
            string msg;

            bool isOk = _productTypeBLL.CreateProductType(productType, out msg);
            //赋值返回
            result.Msg = msg;
            result.IsSuccess = isOk;
            result.Code = isOk ? 200 : result.Code;
            
            return Json(result);

        }
        #endregion

        #region 分页获取类别表的所有数据(GetProductTypeList)
        public IActionResult GetProductTypeList(int page, int limit, string TypeName)
        {
            int count;
            //调用BLL的获取方法
            List<GetProductTypeDTO> list = _productTypeBLL.GetProductTypeList(page, limit, TypeName, out count);

            ReturnResult result = new ReturnResult()
            {
                Code = 0,//layui状态码
                Msg = "获取成功",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);

        }
        #endregion

        #region 删除(DeleteProductType)
        [HttpPost]
        public IActionResult DeleteProductType(string id)
        {
            //实例化返回结果
            ReturnResult result = new ReturnResult();

            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _productTypeBLL.DeleteProductType(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除成功!";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }
        #endregion

        #region 批量删除(DeleteProductTypes)
        [HttpPost]
        public IActionResult DeleteProductTypes(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //判断非空

            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的！";
                return Json(result);
            }
            //调用批量删除的方法
            bool isOk = _productTypeBLL.DeleteProductTypes(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败";
            }
            return Json(result);//返回结果
        }
        #endregion

        #region 修改(UpdateProductType)
        [HttpPost]
        public IActionResult UpdateProductType([FromBody] ProductType productType)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOk = _productTypeBLL.UpdateProductType(productType, out msg);
            result.Msg = msg;
            //判断是否成功
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);//返回结果
        }
        #endregion

        #region 根据id获取信息
        public IActionResult GetProductTypeById(string id)
        {
            //实例化一个返回对象
            ReturnResult result = new ReturnResult();
            if (string.IsNullOrEmpty(id))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //调用方法获取用户的信息
            var userInfo = _productTypeBLL.GetProductTypeById(id);
            //输出对象赋值
            result.Msg = "获取成功！";
            result.Code = 200;
            result.IsSuccess = true;
            result.Data = new
            {
                userInfo,
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 获取所有商品信息
        public IActionResult GetProductType()
        {
            //先通过BLL调用的方法
            List<GetProductTypeDTO> list = _productTypeBLL.GetProductType();
            //实例化返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,         //成功
                Msg = "获取成功",//消息
                Data = list,    //数据
            };
            //返回结果
            return Json(result);
        }
        #endregion

    }
}
